<template>
	<!--新增对话框-->
	<el-dialog
		:title="editForm.id ? '编辑' : '新增'"
		:visible.sync="dialogVisible"
		width="600px"
		:before-close="handleClose"
		:destroy-on-close="true"
	>
		<el-form :model="editForm" :rules="editFormRules" ref="editForm">
			<el-form-item label="报告年份" prop="reportDate" label-width="100px">
                <el-date-picker
                    v-model="editForm.reportDate"
                    type="year"
        			value-format="yyyy"
                    placeholder="选择年份">
                </el-date-picker>
			</el-form-item>
			<el-form-item label="名称" prop="name" label-width="100px">
                <el-input v-model="editForm.name" autocomplete="off"></el-input>
			</el-form-item>
			
			<el-form-item label="省" prop="provinceOptions" label-width="100px">
				<el-cascader
				:options="provinceAndCityData"
				v-model="editForm.provinceOptions">
				</el-cascader>
			</el-form-item>
			<el-form-item label="数量" prop="number" label-width="100px">
                <el-input v-model="editForm.number" autocomplete="off"></el-input>
			</el-form-item>
			<el-form-item label="百分比" prop="percentage" label-width="100px">
                <el-input-number v-model="editForm.percentage" :precision="2" :step="1" :min="0"></el-input-number>
			</el-form-item>
			<el-form-item label="上升下降" prop="lifting" label-width="100px">
				<el-select v-model="editForm.lifting" placeholder="请选择" clearable>
					<el-option label="上升" value="1"></el-option>
					<el-option label="下降" value="2"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="审核状态" prop="verifyStatus" label-width="100px">
				<el-select v-model="editForm.verifyStatus" placeholder="审核状态" clearable>
					<el-option label="未更新" value="WAIT"></el-option>
					<el-option label="已更新" value="PASS"></el-option>
				</el-select>
			</el-form-item>

			<el-form-item label="备注" prop="remark" label-width="100px">
				<el-input v-model="editForm.remark" autocomplete="off"></el-input>
			</el-form-item>
		</el-form>
		<div slot="footer" class="dialog-footer">
			<el-button @click="resetForm('editForm')">取 消</el-button>
			<el-button type="primary" @click="submitForm('editForm')">确 定</el-button>
		</div>
	</el-dialog>
</template>

<script>
import { provinceAndCityData, codeToText } from 'element-china-area-data';
import {formatServerDate} from '@/utils/date';
import TypeSelect from '@/components/typeSelect/Index.vue';
import TypeIdSelect from '@/components/typeIdSelect/Index.vue';
import ReportDatePicker from '@/components/reportDatePicker/Index.vue';
import { isNumber } from '@/utils/validate';
export default {
	name: 'AddUpdateShare',
	components: {
		ReportDatePicker,
		TypeSelect,
		TypeIdSelect,
	},
	data() {
		return {
			editForm: {
				provinceOptions: [],
			},
			loading: false,
			dialogVisible: false,
			editFormRules: {
				reportDate: [{ required: true, message: '请选择报告年月', trigger: 'blur' }],
				capacityNum: [{ required: true, message: '请输入产能数量', trigger: 'blur' }],
			},
			provinceAndCityData: provinceAndCityData.map((item) => {
				const {children, ...rest} = item;
				return {
					...rest
				}
			}),
		};
	},
	mounted() {
		// this.getFarmList('');
	},

	methods: {
		resetForm(formName) {
			this.$refs[formName].resetFields();
			this.dialogVisible = false;
			this.editForm = {
				provinceOptions: [],
			};
		},
		handleClose() {
			this.resetForm('editForm');
		},
		submitForm(formName) {
			this.$refs[formName].validate((valid) => {
				if (valid) {

                    const {reportDate, provinceOptions, ...rest} = this.editForm;
					const params = {
                                reportDate,
                                ...rest,
					};

					if(provinceOptions && provinceOptions[0]){
						params['districtCode'] = `${provinceOptions[0]}0000`;
					}

					this.$axios
						.get('/admin/boat/yellow/type/' + (this.editForm.id ? 'edit' : 'edit'), 
                            {
                                params,
                        })
						.then((res) => {
							this.$message({
								showClose: true,
								message: '恭喜你，操作成功',
								type: 'success',
								onClose: () => {
									this.$emit('refreshDataList');
								},
							});
							this.dialogVisible = false;
							this.resetForm(formName);
						});
				} else {
					console.log('error submit!!');
					return false;
				}
			});
		},
		checkIsDisabledDate(date){
			console.log(date);
			return true;
		},
		init(id) {
			this.dialogVisible = true;
			if (id != null && id != '' && id > 0) {
				this.$axios.get('/admin/boat/yellow/type/detail?id=' + id).then((res) => {
                    const {result = {}} = res;
                    const { report_date, verify_status, district_code, ...rest} = result;

					const provinceOptions = [];
					if(district_code){
						provinceOptions[0] = `${district_code.substring(0, 2)}`;
					}
                    this.editForm = {
						reportDate: report_date,
						verifyStatus:verify_status,
						provinceOptions,
						...rest,
                    };
				});
			}else{
				this.editForm.provinceOptions = [];
			}
		},
	},
};
</script>

<style scoped>
.avatar-uploader .el-upload {
	border: 1px dashed #d9d9d9;
	border-radius: 6px;
	cursor: pointer;
	position: relative;
	overflow: hidden;
}
.avatar-uploader .el-upload:hover {
	border-color: #409eff;
}
.avatar-uploader-icon {
	font-size: 28px;
	color: #8c939d;
	width: 178px;
	height: 178px;
	line-height: 178px;
	text-align: center;
}
.avatar {
	width: 178px;
	height: 178px;
	display: block;
}
</style>
